package com.djl.rabbitmq.config;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: rabbitmq-hello
 * @description: TTL队列配置文件类代码
 * @author: Mr.dai
 * @create: 2021-09-14 10:23
 **/
@Configuration
public class TtlQueueConfig {
    //普通交换机
    public static final String X_EXCHANGE="X";
    //死信交换机
    public static final String Y_DEAD_LETTER_EXCHANGE="Y";
    //普通队列名称
     public static final String QUEUE_A="QA";
     public static final String QUEUE_B="QB";

     //
     public static final String QUEUE_C="QC";

    //死信队列名称
    public static final String DEAD_LETTER_QUEUE ="QD";

    //声明交换机
    @Bean("xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(X_EXCHANGE);
    }
    @Bean("yExchange")
    public DirectExchange yExchange(){
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE );
    }

    //声明队列 ttl
    @Bean("queueA")
    public Queue queueA(){
        Map<String, Object> arguments=new HashMap<>();
        //设置死信交换机
        arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        arguments.put("x-dead-letter-routing-key","YD");
        arguments.put("x-message-ttl",10000);

        return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
    }



    @Bean("queueB")
    public Queue queueB(){
        Map<String, Object> arguments=new HashMap<>();
        //设置死信交换机
        arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        arguments.put("x-dead-letter-routing-key","YD");
        arguments.put("x-message-ttl",40000);

        return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
    }

    //声明QC队列
    @Bean("queueC")
    public Queue queueC(){
        Map<String,Object> arguments=new HashMap<>();
        //设置死信交换机
        arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        //设置私信routing key
        arguments.put("x-dead-letter-routing-key","YD");


        return QueueBuilder.durable(QUEUE_C).withArguments(arguments).build();
    }

    @Bean("queueD")
    public Queue queueD(){
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
    }

    @Bean
    public Binding queueABandingX(@Qualifier("queueA") Queue queueA,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }

    @Bean
    public Binding queueBBandingX(@Qualifier("queueB") Queue queueB,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }
    @Bean
    public Binding queueCBandingX(@Qualifier("queueC") Queue queuec,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queuec).to(xExchange).with("XC");
    }


    @Bean
    public Binding queueDBandingX(@Qualifier("queueD") Queue queueD,
                                  @Qualifier("yExchange") DirectExchange yExchange){
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }

}
